# -*- coding: utf-8 -*-
import json
from urllib import parse
import scrapy
from taobao_sr.items import TaobaoItem
from scrapy_redis.spiders import RedisSpider


class MansuitSpider(RedisSpider):
    name = 'mansuit'
    redis_key = 'mansuit:start_urls'
    # base_url = 'https://s.taobao.com/search?q={}&ie=utf8&sort=sale-desc&p4ppushleft=%2C{}&s={}'
    #
    # def start_requests(self):
    #     key = self.settings['KEY_WORD']
    #     key = parse.quote_plus(key)
    #
    #     page_num = self.settings['PAGE_NUM']
    #     one_page_count = self.settings['ONE_PAGE_COUNT']
    #
    #     for i in range(page_num):
    #         url = self.base_url.format(key,page_num,i*one_page_count)
    #         yield scrapy.Request(url=url,callback=self.parse)

    def parse(self, response):
        config = 'g_page_config = ({.*?});'
        g_page_config = response.selector.re(config)[0]
        g_page_config = json.loads(g_page_config)
        auctions = g_page_config['mods']['itemlist']['data']['auctions']

        for auction in auctions:
            item = TaobaoItem()
            item['user_id'] = auction['user_id']
            item['nid'] = auction['nid']
            item['nick'] = auction['nick']
            item['title'] = auction['raw_title']
            item['price'] = auction['view_price']
            item['loc'] = auction['item_loc']
            item['sales'] = auction['view_sales']
            item['detail_url'] = auction['detail_url']

            yield item
